package com.example.jyjava.controller;

import com.alibaba.fastjson.JSON;
import com.example.jyjava.bean.Sales;
import com.example.jyjava.dao.SalesDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;

@RestController
@RequestMapping("/sales")
public class SalesController {
    @Autowired
    private SalesDao salesDao;

    @RequestMapping("/addSales")
    public String addSales(@RequestBody Sales sales){
        String falg = "400";
        sales.setCreatTime(new Date());
        sales.setUpdateTime(new Date());
        sales.setDeleteType(false);
        int i = salesDao.addSales(sales);
        HashMap<String, Object> res = new HashMap<>();
        if (i > 0){
            falg = "200";
            res.put("flag",falg);
            res.put("collectNum",i);
        }else{
            res.put("flag",falg);
        }
        String s = JSON.toJSONString(res);
        return s;
    }

    @RequestMapping("/getSalesId")
    public String getSalesId(@RequestBody Sales sales){
        String falg = "400";
        Sales sa = salesDao.getSalesId(sales.getOrderId());
        HashMap<String, Object> res = new HashMap<>();
        if (sa != null){
            falg = "200";
            res.put("flag",falg);
            res.put("sales",sa);
        }else{
            res.put("flag",falg);
        }
        String s = JSON.toJSONString(res);
        return s;
    }
}
